package com.css.fxfzaqbz.modules.base.service.impl;

import cn.hutool.core.lang.hash.Hash;
import com.css.fxfzaqbz.modules.base.entity.HouseDestoryEntity;
import com.css.fxfzaqbz.modules.base.repository.CountProcessRepository;
import com.css.fxfzaqbz.modules.base.service.CountProcessService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CountProcessServiceImpl implements CountProcessService {
    @Resource
    private CountProcessRepository countProcessRepository;
    @Override
    public Map<String, Object> getPage(int curPage, int pageSize,String sdaId) {
        return countProcessRepository.getPage(curPage,pageSize,sdaId);
    }

    @Override
    public Map<String, String> statistics(String sdaId) {
        List<HouseDestoryEntity> entityList=countProcessRepository.statistics(sdaId);
        Map<String, String> map = new HashMap<>();
        if (entityList.size()==0){
            map.put("three","0");
            map.put("four","0");
            return map;
        }
        int[] numbers = new int[entityList.size()];
        for (int i = 0; i < entityList.size(); i++) {
            HouseDestoryEntity entity = entityList.get(i);
            //最大破坏类型 0表示基本破坏 1轻微破坏 2中等破坏 3严重破坏 4毁坏
            numbers[i] =Integer.parseInt(entity.getMaxDestoryType());
        }
        //3严重破坏
        String three = calculatePercentage(numbers, 3);
        //4损坏
        String four = calculatePercentage(numbers, 4);
        map.put("three",three);
        map.put("four",four);
        return map;
    }



    public static String calculatePercentage(int[] numbers, int target) {
        int count = 0;
        int total = numbers.length;

        for (int number : numbers) {
            if (number == target) {
                count++;
            }
        }

        double percentage = (double) count / total * 100;
        // 格式化百分比为两位小数
        DecimalFormat df = new DecimalFormat("0.00");
        String formattedPercentage = df.format(percentage);
        return formattedPercentage;
    }

    @Override
    public List<HouseDestoryEntity> exportExcel(String id) {
        return countProcessRepository.statistics(id);
    }
}
